<!DOCTYPE html>
<!--
Copyright 2017 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/components/app-route/app-location.html">
<link rel="import" href="/components/iron-form/iron-form.html">
<link rel="import" href="/components/paper-button/paper-button.html">
<link rel="import" href="/components/paper-dialog/paper-dialog.html">
<link rel="import" href="/components/paper-fab/paper-fab.html">
<link rel="import" href="/components/paper-input/paper-input.html">
<link rel="import" href="/components/paper-tooltip/paper-tooltip.html">

<link rel="import" href="/elements/base-style.html">


<dom-module id="patch-job-fab">
  <template>
    <style include="base-style">
      paper-dialog {
        width: 40em;
      }

      paper-fab {
        color: var(--paper-grey-700);
        --paper-fab-background: white;
      }

      .error {
        color: var(--paper-red-500);
      }
    </style>

    <app-location route="{{route}}"></app-location>
    <paper-fab mini id="try-fab" icon="forward" on-tap="openDialog"></paper-fab>
    <paper-tooltip for="try-fab" position="left">Test a patch</paper-tooltip>
    <paper-dialog id="try_dialog" entry-animation="fade-in-animation" exit-animation="fade-out-animation">
      <h2>Test a patch with this configuration</h2>
      <div>
        <p>
          See the impact of a patch on this job's metric. A patch job runs HEAD
          and HEAD + patch with the same configuration and metric as this job.
        </p>
        <template is="dom-if" if="[[error]]">
          <p class="error">[[error]]
        </template>
        <iron-form id="try_form" headers="[[authHeaders]]" on-iron-form-error="handleError" on-iron-form-response="handleResponse">
          <form action="/api/new" method="POST">
            <template is="dom-repeat" items="[[getArguments(job)]]">
              <input type="hidden" name="[[item.key]]" value="[[item.value]]">
            </template>
            <paper-input name="patch" label="Gerrit URL" required></paper-input>
          </form>
        </iron-form>
      </div>
      <div class="buttons">
        <paper-button on-tap="submit">Start</paper-button>
      </div>
    </paper-dialog>
  </template>

  <script>
    'use strict';
    Polymer({
      is: 'patch-job-fab',

      properties: {
        error: Object,
        job: Object,
      },

      getArguments(job) {
        if (!job) {
          return [];
        }
        const args = Object.assign({}, job.arguments);

        // We really want a try job, not a bisection.
        args.comparison_mode = 'try';
        args.base_git_hash = 'HEAD';
        args.end_git_hash = 'HEAD';

        // Try jobs don't support the `start_git_hash` argument.
        delete args.start_git_hash;
        delete args.name;
        delete args.patch;
        delete args.user;

        const tuples = [];
        for (const key in args) {
          tuples.push({key, value: args[key]});
        }
        return tuples;
      },

      openDialog() {
        this.$.try_dialog.open();
      },

      submit() {
        this.$.try_form.submit();
      },

      handleError(event) {
        this.error = event.detail.request.response.error;
      },

      handleResponse(event) {
        this.set('route.path', '/job/' + event.detail.response.jobId);
        this.$.try_dialog.close();
      },
    });
  </script>
</dom-module>
